﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class SpecialExpertDAL : ISpecialExpertDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public SpecialExpertDAL()
        {
        }
        public DataSet LoadSpecialExpert(Nullable<int> Sequence, Nullable<int> EmpSequence)
        {
            string strSQL;
            int nSequence = Sequence.HasValue ? (int)Sequence : 0;
            int nEmpSequence = EmpSequence.HasValue ? (int)EmpSequence : 0;

            DataSet ds = null;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", nSequence);
                dbManager.AddParameters(2, "@nEmpSequence", nEmpSequence);

                strSQL = "SELECT e.EMP_EXP_SEQ, e.EMP_SEQ, e.EXP_TYPE_SEQ, e.EXP_DESCR, e.REMARKS"
                        + ", et.EXP_TYPE_NAME, e.RECORD_STATUS, e.CREATED_BY, e.UPDATED_BY"
                    + " FROM HRM_PS_EMP_SPECIAL_EXPERTS e"
                    + " LEFT JOIN MAS_EXPERT_TYPES et ON e.EXP_TYPE_SEQ = et.EXP_TYPE_SEQ"
                    + " WHERE e.RECORD_STATUS = @ActiveRecord"
                    + (nSequence > 0 ? " AND e.EMP_EXP_SEQ = @Sequence" : "")
                    + (nEmpSequence > 0 ? " AND e.EMP_SEQ = @EmpSeq" : "")
                    + " ORDER BY e.EMP_EXP_SEQ";
                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return ds;
        }
        public void Insert(int EmpSequence, int ExpertType, string ExpertDescr, string Remarks, int CreatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@EmpSequence", EmpSequence);
                dbManager.AddParameters(1, "@ExpertType", ExpertType);
                dbManager.AddParameters(2, "@ExpertDescr", ExpertDescr);
                dbManager.AddParameters(3, "@Remarks", Remarks);
                dbManager.AddParameters(4, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(6, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_EMP_SPECIAL_EXPERTS"
                    + "(EMP_SEQ, EXP_TYPE_SEQ, EXP_DESCR, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES (@EmpSequence, @ExpertType, @ExpertDescr, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(int Sequence, int ExpertType, string ExpertDescr, string Remarks, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@ExpertType", ExpertType);
                dbManager.AddParameters(2, "@ExpertDescr", ExpertDescr);
                dbManager.AddParameters(3, "@Remarks", Remarks);
                dbManager.AddParameters(4, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_EMP_SPECIAL_EXPERTS"
                        + " SET EXP_TYPE_SEQ = @ExpertType"
                        + ", EXP_DESCR = @ExpertDescr"
                        + ", REMARKS = @Remarks"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE EMP_EXP_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_EMP_SPECIAL_EXPERTS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE EMP_EXP_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
